//
//  ResetTelViewController.m
//  CarCare
//
//  Created by ileo on 14-8-19.
//  Copyright (c) 2014年 baozun. All rights reserved.
//

#import "ResetTelViewController.h"

#import "UserHelper.h"
#import "AIUserInfo.h"

@interface ResetTelViewController ()

@property (nonatomic, strong) UITextField *telTextField;
@property (nonatomic, strong) UITextField *codeTextField;
@property (nonatomic, strong) UIButton *codeButton;

@property (nonatomic , strong) UILabel * codeButtonOnLabel;

@end

@implementation ResetTelViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"手机号绑定";
    
    __weak __typeof(self) wself = self;
    [self navSetupLeftTitle:@"取消" action:^{
        [wself.view endEditing:YES];
        [wself dismissViewControllerAnimated:YES completion:nil success:NO info:nil];
    }];
    [self navSetupRightTitle:@"绑定" action:^{
        [wself.view endEditing:YES];
        if ([Check isTel:wself.telTextField.text] && [Check isCode:wself.codeTextField.text]) {
            [[[NetUser setNewTel:wself.telTextField.text code:wself.codeTextField.text] requestWithLoad:RequestLoadShowLoading|RequestLoadShowErrorTips] requestSuccess:^(id JSON) {
                USERINFO.tel = wself.telTextField.text;
                USERINFO.userID = [JSON[@"newId"] integerValue];
                NSString *newSession = JSON[@"newSession"];
                if ([newSession isNotNil]) {
                    USERINFO.session = newSession;
                }
                TipsView *tips = [TipsView okTipsWithTitle:@"" lightTitles:@[@"手机号绑定成功"]];
                [tips addOKButton:LOCSTRING(@"ok") clickOK:^{
                    [AlertMgr dismiss:TYPE_VIEW_ALERT];
                    [wself dismissViewControllerAnimated:YES completion:nil success:YES info:nil];
                }];
                [AlertMgr showView:tips];
            }];
        }
    }];
    
    self.telTextField = [self textFieldWithFrame:FactoryRectDefault(TOP_SIDE) type:TEXTFIELD_TEL];
    self.telTextField.placeholder = LOCSTRING(@"fill_new_tel");
    
    self.codeTextField = [self textFieldWithFrame:FactoryRectDefault(self.telTextField.bottom + GAP_HEIGHT) type:TEXTFIELD_CODE];
    

    UIButton * callMessageButton = [[UIButton buttonWithFrame:CGRectMake(__MAIN_WIDTH - 120, self.codeTextField.bottom, 120, 50) title:@"收不到短信验证码？" textColor:[Theme wordColor] font:FONT_BUTTON click:^{
        //确认  打电话纪录语音信息  取消  不做处理
        TipsView *tipsV = [TipsView voiceTipsWithTitle:nil lightTitles:@[@"验证码将会以电话方式播报给您，请注意来电，并记下电话中报的验证码"]];
        [tipsV addLeftButton:@"取消" clickLeft:^{
            [AlertMgr dismiss:TYPE_VIEW_ALERT];
        } rightButton:@"获取验证码" clickRight:^{
            [wself callMessageCodeClick];
            [AlertMgr dismiss:TYPE_VIEW_ALERT];
        }];
        [AlertMgr showView:tipsV];
    }] setupOnView:self.view];
    
    callMessageButton.titleLabel.font = FONT_DEFAULT_Light(12);
    callMessageButton.titleLabel.textAlignment = NSTextAlignmentRight;
    callMessageButton.hidden = YES;

    self.codeButton = [ViewFactory addButtonType:BUTTON_CODE onTextField:self.codeTextField inView:self.view action:^{
        callMessageButton.hidden = NO;
        [wself clickOnCode];
    } ok:nil];
    self.codeButtonOnLabel = [Factory labelWithFrame:self.codeButton.frame font:FONT_DEFAULT_Light(14) text:self.codeButton.titleLabel.text textColor:[UIColor grayColor] onView:self.view textAlignment:self.codeButton.titleLabel.textAlignment];
    self.codeButtonOnLabel.hidden = ([self.telTextField.text isTel] ? YES : NO);
    self.codeButton.hidden = ([self.telTextField.text isTel] ? NO : YES);
    
    // Do any additional setup after loading the view.
}
-(void)textFieldTextChanged:(UITextField *)textField
{
    if (textField == self.telTextField) {
        [self.codeButtonOnLabel setHidden:([textField.text isTel] ? YES : NO)];
        [self.codeButton setHidden:([textField.text isTel] ? NO : YES)];
    }
}
- (NSInteger)textFieldMaxNumberWithTextField:(UITextField *)textField
{
    if (textField == self.telTextField) {
        return 10;
    }
    else if (textField == self.codeTextField)
    {
        return 7;
    }
    else{
        return [super textFieldMaxNumberWithTextField:textField];
    }
    
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.telTextField becomeFirstResponder];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)callMessageCodeClick
{
    if(![self.telTextField.text isEqualToString:USERINFO.tel]){
        if ([Check isTel:self.telTextField.text]) {
            [UserHelper callMessageCodeByTel: self.telTextField.text success:nil fail:nil];
        }
    }else{
        [AlertMgr showShortWarnTextTips:@[@"请输入新手机号"]];
    }
}
-(void)clickOnCode{
    if(![self.telTextField.text isEqualToString:USERINFO.tel]){
        if ([Check isTel:self.telTextField.text]) {
            self.codeButton.enabled = NO;
            __weak __typeof(self) wself = self;
            [UserHelper getCodeByTel:self.telTextField.text  success:^{
            } fail:^{
                wself.codeButton.enabled = YES;
            }];
        }
    }else{
        [AlertMgr showShortWarnTextTips:@[@"请输入新手机号"]];
    }
}


@end
